package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponExample;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.MarketCouponUserExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.service.AdminCouponService;
import com.cskaoyan.service.AdminCouponServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/admin/coupon/*")
public class AdminCouponServlet extends WdBaseServlet {
    private AdminCouponService couponService = new AdminCouponServiceImpl();

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        Short type = request.getParameter("type") == null ? null : Short.parseShort(request.getParameter("type"));
        //0——正常  1——已过期  2——已下架
        Short status = request.getParameter("status") == null ? null : Short.parseShort(request.getParameter("status"));

        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        CommonData<MarketCoupon> commonData = couponService.list(name, type, status, page, limit, sort, order);
        ResponseUtil.responseJson(response, BaseRespVo.ok(commonData));
    }

    @SneakyThrows
    private void create(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketCoupon read = JacksonUtil.read(jsonStr, MarketCoupon.class);

        read.setUpdateTime(new Date());
        read.setAddTime(new Date());

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        mapper.insertSelective(read);
        sqlSession.commit();
        sqlSession.close();

        ResponseUtil.responseJson(response, BaseRespVo.ok(read));
    }

    @SneakyThrows
    private void update(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketCoupon coupon = JacksonUtil.read(jsonStr, MarketCoupon.class);
        coupon.setUpdateTime(new Date());

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        mapper.updateByPrimaryKey(coupon);
        sqlSession.commit();
        sqlSession.close();

        ResponseUtil.responseJson(response, BaseRespVo.ok(coupon));
    }

    @SneakyThrows
    private void delete(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketCoupon coupon = JacksonUtil.read(jsonStr, MarketCoupon.class);
        coupon.setDeleted(true);
        Integer id = coupon.getId();
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        mapper.updateByExampleSelective(coupon, example);
        sqlSession.commit();
        sqlSession.close();

        ResponseUtil.responseJson(response, BaseRespVo.ok());
    }

    private void read(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = mapper.selectByPrimaryKey(id);
        sqlSession.close();
        ResponseUtil.responseOkJsonVo(response, marketCoupon);

    }
    private void listuser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String couponIdStr = request.getParameter("couponId");
        String userIdStr = request.getParameter("userId");
        String statusStr = request.getParameter("status");

        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        if (couponIdStr != null) {
            criteria.andUserIdEqualTo(Integer.parseInt(couponIdStr));
        }
        if (userIdStr != null) {
            criteria.andUserIdEqualTo(Integer.parseInt(userIdStr));
        }
        if (statusStr != null) {
            criteria.andStatusEqualTo(Short.parseShort(statusStr));
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponUserMapper mapper = sqlSession.getMapper(MarketCouponUserMapper.class);

        PageHelper.startPage(page, limit);
        List<MarketCouponUser> marketCouponsUser = mapper.selectByExample(example);
        sqlSession.close();

        CommonData commonData = CommonData.data(new PageInfo(marketCouponsUser));
        ResponseUtil.responseJson(response, BaseRespVo.ok(commonData));
    }
}

